package top.ninwoo.cloud.ipservice.service.impl;

import org.springframework.stereotype.Service;
import top.ninwoo.cloud.ipservice.service.IpService;

import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicInteger;

@Service
public class IpServiceImpl implements IpService {
    HashMap<String, String> ipMap = new HashMap<>();
    HashMap<Long, HashMap<String, AtomicInteger>> countMap = new HashMap<>();

    // 存储app对应的全部的ip地址
    HashMap<Long, Map<String, Set<String>>> appIp = new HashMap<>();


    /**
     * 分配IP地址
     * @param clusterId
     * @param appName
     * @param containerId
     * @param ipRange
     * @return
     */
    @Override
    public String assignIp(long clusterId, String appName, String containerId, String ipRange) {
        if(!countMap.containsKey(clusterId)) {
            countMap.put(clusterId, new HashMap<>());
        }
        HashMap<String, AtomicInteger> clusterCountMap = countMap.get(clusterId);

        AtomicInteger atomicInteger;
        if(ipMap.containsKey(containerId)) {
            return ipMap.get(containerId);
        }
        if(!clusterCountMap.containsKey(ipRange)) {
            clusterCountMap.put(ipRange, new AtomicInteger(2));
        }
        atomicInteger = clusterCountMap.get(ipRange);
        String ip = ipRange.substring(0, ipRange.length() - 4) + atomicInteger.getAndIncrement()
                + ipRange.substring(ipRange.length()-3);
        ipMap.put(containerId, ip);

        // 放到appIp中
        if (!appIp.containsKey(clusterId)) {
            appIp.put(clusterId, new HashMap<>());
        }
        Map<String, Set<String>> clusterAppId = appIp.get(clusterId);
        if (!clusterAppId.containsKey(appName)) {
            clusterAppId.put(appName, new HashSet<>());
        }
        Set<String> ipSet = clusterAppId.get(appName);
        ipSet.add(ip);
        return ip;
    }

    @Override
    public void deleteIp(long clusterId, String appName, String containerId, String ip) {
        // TODO: 这里应该在加入一个还回ip的功能
        if(!ipMap.containsKey(containerId)) {
            return;
        }
        ipMap.remove(containerId);
        if(!appIp.containsKey(clusterId)) {
            return;
        }
        Map<String, Set<String>> clusterAppId = appIp.get(clusterId);
        if(!clusterAppId.containsKey(appName)) {
            return;
        }
        clusterAppId.get(appName).remove(ip);
    }

    /**
     * 通过容器id获取ip地址
     * @param containerId
     * @return
     */
    @Override
    public String getContainerIp(String containerId) {
        if(!ipMap.containsKey(containerId)) {
            return "";
        }
        return ipMap.get(containerId);
    }

    @Override
    public Set<String> getIpListByAppName(long clusterId, String appName) {
        if (!appIp.containsKey(clusterId)) {
            return new HashSet<>();
        }

        Map<String, Set<String>> clusterAppIp = appIp.get(clusterId);
        if (!clusterAppIp.containsKey(appName)) {
            return new HashSet<>();
        }

        return clusterAppIp.get(appName);
    }

    // TODO: 差一个把ip还回去的功能模块


    @Override
    public String clearIpByClusterId(long clusterId) {
        ipMap.clear();
        appIp.clear();
        countMap.clear();
        return "success";
    }
}